<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Shop_Order_Model extends MY_Model {
    public function __construct() {
        parent::__construct();
        $this->load->model('entity/order_entity', 'Order_Entity');
    }

    public function countOrder($shop_id, $option = array()) {
        $this->db->select('a.id', true);
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->where('a.shop_id', $shop_id);
        if (count($option) > 0) {
            $this->db->where($option);
        }
        return $this->db->count_all_results();
    }

    public function paging($shop_id, $limit, $offset, $option = array()) {
        $select = "a.id, a.shop_id, a.name, a.email, a.phone, a.address,
        a.receiver_name, a.receiver_email, a.receiver_phone, a.receiver_address,
        a.note, a.created_at, a.deleted_at,
        c.title as shop_title, c.slug as shop_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->where('a.shop_id', $shop_id);
        if (count($option) > 0) {
            $this->db->where($option);
        }
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->limit($limit, $offset);
        $result = $this->db->get()->result('Order_Entity');
        return $result;
    }

    public function billing($aShop, $options) {
        $date = new DateTime();
        $createdAt = $date->format('Y-m-d H:i:s');
        $this->db->trans_begin();
        foreach ($aShop as $shop_id => $shop) {
            $order = array(
                'shop_id'    => $shop_id,
                'name'       => $options['name'],
                'phone'      => $options['phone'],
                'address'    => $options['address'],
                'note'       => $options['note'],
                'created_at' => $createdAt
                );
            if(isset($options['user_id'])){
                $order['user_id'] = $options['user_id'];
            }
            $this->db->insert($this->table_order_name, $order);
            $orderId = $this->db->insert_id();
            foreach ($shop as $product) {
                $orderDetail = array(
                    'order_id' => $orderId,
                    'product_id' => $product['id'],
                    'product_code' => $product['options']['code'],
                    'price' => $product['price'],
                    'count' => $product['qty'],
                    );
                $this->db->insert($this->table_order_detail_name, $orderDetail);
            }
        }
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function get($shop_id, $id) {
        $this->load->model('entity/order_detail_entity', 'Order_Detail_Entity');
        $select = "a.id, a.product_id, a.product_code, a.price, a.count,
        b.image as product_image ,
        c.title as product_title, c.slug as product_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_order_detail_name . ' a');
        $this->db->join($this->table_product_details_name . ' b', 'a.product_id=b.product_id and a.product_code=b.code');
        $this->db->join($this->table_product_name . ' c', 'c.id=b.product_id');
        $this->db->join($this->table_order_name . ' d', 'a.order_id=d.id');
        $this->db->where('a.order_id', $id);
        $this->db->where('d.shop_id', $shop_id);
        return $this->db->get()->result('Order_Detail_Entity');
    }

    public function countSearchOrder($shop_id, $seach_key, $option=array()){
        $this->db->select('a.id, a.shop_id');
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        if (count($option) > 0) {
            $this->db->where($option);
        } else {
            $this->db->like('a.created_at', $seach_key);
            $this->db->or_like('a.note', $seach_key);
            $this->db->or_like('a.name', $seach_key);
            $this->db->or_like('a.phone', $seach_key);
            $this->db->or_like('a.address', $seach_key);
            $this->db->or_like('a.email', $seach_key);
            $this->db->or_like('a.receiver_email', $seach_key);
            $this->db->or_like('a.receiver_name', $seach_key);
            $this->db->or_like('a.receiver_phone', $seach_key);
            $this->db->or_like('a.receiver_address', $seach_key);
        }
        $this->db->having('a.shop_id', $shop_id);
        return count($this->db->get()->result());
    }

    public function searchPaging($shop_id, $seach_key, $limit, $offset, $option=array()){
        $select = "a.id, a.shop_id, a.name, a.email, a.phone, a.address,
                    a.receiver_name, a.receiver_email, a.receiver_phone, a.receiver_address,
                    a.note, a.created_at, a.deleted_at,
                    c.title as shop_title, c.slug as shop_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        if (count($option) > 0) {
            $this->db->where($option);
        } else {
            $this->db->like('a.created_at', $seach_key);
            $this->db->or_like('a.note', $seach_key);
            $this->db->or_like('a.name', $seach_key);
            $this->db->or_like('a.phone', $seach_key);
            $this->db->or_like('a.address', $seach_key);
            $this->db->or_like('a.email', $seach_key);
            $this->db->or_like('a.receiver_email', $seach_key);
            $this->db->or_like('a.receiver_name', $seach_key);
            $this->db->or_like('a.receiver_phone', $seach_key);
            $this->db->or_like('a.receiver_address', $seach_key);
        }
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->having('a.shop_id', $shop_id);
        $this->db->limit($limit, $offset);
        $result = $this->db->get()->result('Order_Entity');
        return $result;
    }
}
?>